package com.atgouwu.scmall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * 跨域：方式2,精细高效
 */
@Configuration
public class CorsConfig {

    @Bean
    public CorsWebFilter corsWebFilter(){
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); //基于URL的跨域配置源对象
        CorsConfiguration corsConfiguration = new CorsConfiguration(); //创建跨域配置
        corsConfiguration.addAllowedHeader("*"); //允许任意请求头
        corsConfiguration.addAllowedMethod("*"); //允许任意请求方法
        //corsConfiguration.addAllowedOrigin("*"); //允许任意来源的请求(旧版本)
        corsConfiguration.addAllowedOriginPattern("*"); //允许任意来源的请求(新版本)
        corsConfiguration.setAllowCredentials(true); //允许cookie跨域,否则会丢失
        source.registerCorsConfiguration("/**",corsConfiguration); //任何请求都进行跨域处理
        return new CorsWebFilter(source);
    }

}
